﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Domain
{
    public class PhenotypeDataSchemaBuilder
    {
        #region Private Members

        private IList<DataColumnTypeResolver> _columnResolvers;

        #endregion

        #region Constructors

        public PhenotypeDataSchemaBuilder()
        {
            this._columnResolvers = new List<DataColumnTypeResolver>();
        }

        #endregion

        #region Public Methods

        public virtual void SetColumnNames(IEnumerable<string> columnNames)
        {
            this._columnResolvers.Clear();

            if (columnNames != null)
            {
                foreach (string columnName in columnNames)
                {
                    this._columnResolvers.Add(new DataColumnTypeResolver(columnName));
                }
            }
        }

        public virtual void AddRow(IEnumerable<string> row)
        {
            if (row != null)
            {
                int columnIndex = 0;

                foreach (string value in row)
                {
                    if (columnIndex < this._columnResolvers.Count)
                    {
                        //update column info and move to next column
                        this._columnResolvers[columnIndex++].AddValue(value);
                    }
                }
            }
        }

        #endregion

        #region Properties

        public PhenotypeFileDataSchema Product
        {
            get
            {
                PhenotypeFileDataSchema schema = new PhenotypeFileDataSchema();

                foreach (DataColumnTypeResolver resolver in this._columnResolvers)
                {
                    schema.AddColumn(resolver.CreateColumnType());
                }

                return schema;
            }
        }

        #endregion
    }
}
